//
//  ByCellShopTopPartSubclass.m
//  mijifen_ios
//
//  Created by 朱凯 on 2017/10/31.
//  Copyright © 2017年 itboye. All rights reserved.
//

#import "ByCellShopSecondPartList.h"

@implementation ByCellShopSecondPartList

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

- (void)setupUI {
    
    [self.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    NSArray *titles = _parts;
    UIControl *lastView = nil;
    UIControl *topView = nil;
    for (int i = 0; i < titles.count; i ++) {
        
        UIControl *basalView = [UIControl new];
        [self.contentView addSubview:basalView];
        basalView.tag = i;
        [basalView addTarget:self action:@selector(touchAction:) forControlEvents:UIControlEventTouchUpInside];
        
        // 图片
        UIImageView *iconView = [UIImageView new];
        iconView.tag = 1;
        iconView.backgroundColor = BY_BLUE_COLOR;
        [basalView addSubview:iconView];
        // 文字
        UILabel *label = [[UILabel alloc] init];
        label.adjustsFontSizeToFitWidth = true;
        label.text = titles[i];
        label.font = FONT(14);
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = [UIColor blackColor];
        [basalView addSubview:label];
        
        // 布局
        [basalView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(self.contentView).multipliedBy(1.0 / 4);
            make.height.equalTo(basalView.mas_width);
            // 每行第一个
            if (i % 4 == 0) {
                make.left.equalTo(@0);
            } else {
                make.left.equalTo(lastView.mas_right);
            }
            // 第一行
            if (i / 4 == 0) {
                make.top.equalTo(@10);
            } else {
                make.top.equalTo(topView.mas_bottom).offset(10);
            }
        }];
        
        [iconView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(basalView).multipliedBy(0.6);
            make.height.equalTo(iconView.mas_width);
            make.top.equalTo(@0);
            make.centerX.equalTo(@0);
        }];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(@0);
            make.top.equalTo(iconView.mas_bottom);
        }];
        
        
        if (i == 0) {
            topView = basalView;
        }
        lastView = basalView;
    }
    
    
    [lastView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.contentView);
    }];
}

- (void)setParts:(NSArray *)parts {
    
    _parts = parts;
    
    [self setupUI];
}

- (void)touchAction:(UIControl *)sender {
    
    if (self.didSelectPart) {
        self.didSelectPart(sender.tag);
    }
}
@end
